<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Introduction - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.view.introduction.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.view.introduction.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.view.html">Zend_View</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.view.html">Zend_View</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.view.controllers.html">Controller Scripts</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.view.introduction" class="section"><div class="info"><h1 class="title">Introduction</h1></div>
    

    <p class="para">
        <span class="classname">Zend_View</span> is a class for working with the &quot;view&quot; portion of
        the model-view-controller pattern. That is, it exists to
        help keep the view script separate from the model and
        controller scripts. It provides a system of helpers, output
        filters, and variable escaping.
    </p>

    <p class="para">
        <span class="classname">Zend_View</span> is template system agnostic; you may use
        <acronym class="acronym">PHP</acronym> as your template language, or create instances of other
        template systems and manipulate them within your view
        script.
    </p>

    <p class="para">
        Essentially, using <span class="classname">Zend_View</span> happens in two major steps:
        1. Your controller script creates an instance of
        <span class="classname">Zend_View</span> and assigns variables to that instance.
        2. The controller tells the <span class="classname">Zend_View</span> to render a particular
        view, thereby handing control over the view script, which
        generates the view output.
    </p>

    <div class="section" id="zend.view.introduction.controller"><div class="info"><h1 class="title">Controller Script</h1></div>
        

        <p class="para">
            As a simple example, let us say your controller has a list
            of book data that it wants to have rendered by a view. The
            controller script might look something like this:
        </p>

        <pre class="programlisting brush: php">
// use a model to get the data for book authors and titles.
$data = array(
    array(
        &#039;author&#039; =&gt; &#039;Hernando de Soto&#039;,
        &#039;title&#039; =&gt; &#039;The Mystery of Capitalism&#039;
    ),
    array(
        &#039;author&#039; =&gt; &#039;Henry Hazlitt&#039;,
        &#039;title&#039; =&gt; &#039;Economics in One Lesson&#039;
    ),
    array(
        &#039;author&#039; =&gt; &#039;Milton Friedman&#039;,
        &#039;title&#039; =&gt; &#039;Free to Choose&#039;
    )
);

// now assign the book data to a Zend_View instance
Zend_Loader::loadClass(&#039;Zend_View&#039;);
$view = new Zend_View();
$view-&gt;books = $data;

// and render a view script called &quot;booklist.php&quot;
echo $view-&gt;render(&#039;booklist.php&#039;);
</pre>

    </div>

    <div class="section" id="zend.view.introduction.view"><div class="info"><h1 class="title">View Script</h1></div>
        

        <p class="para">
            Now we need the associated view script, &quot;booklist.php&quot;.
            This is a <acronym class="acronym">PHP</acronym> script like any other, with one exception: it
            executes inside the scope of the <span class="classname">Zend_View</span> instance, which
            means that references to $this point to the <span class="classname">Zend_View</span>
            instance properties and methods. (Variables assigned to the
            instance by the controller are public properties of the
            <span class="classname">Zend_View</span> instance). Thus, a very basic view script could
            look like this:
        </p>

        <pre class="programlisting brush: php">
 if ($this-&gt;books): ?&gt;

    &lt;!-- A table of some books. --&gt;
    &lt;table&gt;
        &lt;tr&gt;
            &lt;th&gt;Author&lt;/th&gt;
            &lt;th&gt;Title&lt;/th&gt;
        &lt;/tr&gt;

        &lt;?php foreach ($this-&gt;books as $key =&gt; $val): ?&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;?php echo $this-&gt;escape($val[&#039;author&#039;]) ?&gt;&lt;/td&gt;
            &lt;td&gt;&lt;?php echo $this-&gt;escape($val[&#039;title&#039;]) ?&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;?php endforeach; ?&gt;

    &lt;/table&gt;

&lt;?php else: ?&gt;

    &lt;p&gt;There are no books to display.&lt;/p&gt;

&lt;?php endif;?&gt;
</pre>


        <p class="para">
            Note how we use the &quot;escape()&quot; method to apply output
            escaping to variables.
        </p>
    </div>

    <div class="section" id="zend.view.introduction.options"><div class="info"><h1 class="title">Options</h1></div>
        

        <p class="para">
            <span class="classname">Zend_View</span> has several options that may be set to
            configure the behaviour of your view scripts.
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                    <span class="property">basePath</span>: indicate a base path from which to set
                    the script, helper, and filter path. It assumes a directory
                    structure of:
                </p>

                <pre class="programlisting brush: php">
base/path/
    helpers/
    filters/
    scripts/
</pre>


                <p class="para">
                    This may be set via  <span class="methodname">setBasePath()</span>,
                     <span class="methodname">addBasePath()</span>, or the <span class="property">basePath</span>
                    option to the constructor.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    <span class="property">encoding</span>: indicate the character encoding to use
                    with  <span class="methodname">htmlentities()</span>,
                     <span class="methodname">htmlspecialchars()</span>, and other operations. Defaults
                    to UTF-8. May be set via
                     <span class="methodname">setEncoding()</span> or the <span class="property">encoding</span>
                    option to the constructor.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    <span class="property">escape</span>: indicate a callback to be used by
                     <span class="methodname">escape()</span>. May be set via
                     <span class="methodname">setEscape()</span> or the <span class="property">escape</span> option
                    to the constructor.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    <span class="property">filter</span>: indicate a filter to use after rendering
                    a view script. May be set via  <span class="methodname">setFilter()</span>,
                     <span class="methodname">addFilter()</span>, or the <span class="property">filter</span> option
                    to the constructor.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    <span class="property">strictVars</span>: force <span class="classname">Zend_View</span> to emit
                    notices and warnings when uninitialized view variables are
                    accessed. This may be set by calling
                     <span class="methodname">strictVars(true)</span> or passing the
                    <span class="property">strictVars</span> option to the constructor.
                </p>
            </li>
        </ul>
    </div>

    <div class="section" id="zend.view.introduction.shortTags"><div class="info"><h1 class="title">Short Tags with View Scripts</h1></div>
        

        <p class="para">
            In our examples, we make use of <acronym class="acronym">PHP</acronym> long tags:
            <em class="emphasis">&lt;?php</em>. We also favor the use of <a href="http://us.php.net/manual/en/control-structures.alternative-syntax.php" class="link external">&raquo; alternate
                syntax for control structures</a>. These are convenient shorthands to use when
            writing view scripts, as they make the constructs more terse, keep statements on single
            lines, and eliminate the need to hunt for brackets within <acronym class="acronym">HTML</acronym>.
        </p>

        <p class="para">
            In previous versions, we often recommended using short tags (<em class="emphasis">&lt;?</em>
            and <em class="emphasis">&lt;?=</em>), as they make the view scripts slightly less verbose.
            However, the default for the <var class="filename">php.ini</var>
            <b><tt>short_open_tag</tt></b> setting is typically off in production or on shared
            hosts -- making their use not terribly portable. If you use template
            <acronym class="acronym">XML</acronym> in view scripts, short open tags will cause the templates to fail
            validation. Finally, if you use short tags when <b><tt>short_open_tag</tt></b> is
            off, the view scripts will either cause errors or simply echo <acronym class="acronym">PHP</acronym>
            code back to the viewer.
        </p>

        <p class="para">
            If, despite these warnings, you wish to use short tags but they are disabled, you have
            two options:
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                    Turn on short tags in your <var class="filename">.htaccess</var> file:
                </p>

                <pre class="programlisting brush: apache">
php_value &quot;short_open_tag&quot; &quot;on&quot;
</pre>


                <p class="para">
                    This will only be possible if you are allowed to create and
                    utilize <var class="filename">.htaccess</var> files. This directive can
                    also be added to your <var class="filename">httpd.conf</var> file.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                    Enable an optional stream wrapper to convert short tags to
                    long tags on the fly:
                </p>

                <pre class="programlisting brush: php">
$view-&gt;setUseStreamWrapper(true);
</pre>


                <p class="para">
                    This registers <span class="classname">Zend_View_Stream</span> as a stream
                    wrapper for view scripts, and will ensure that your code
                    continues to work as if short tags were enabled.
                </p>
            </li>
        </ul>

        <div class="warning"><b class="warning">Warning</b><div class="info"><h1 class="title">View Stream Wrapper Degrades Performance</h1></div>
            

            <p class="para">
                Usage of the stream wrapper <em class="emphasis">will</em> degrade
                performance of your application, though actual benchmarks are
                unavailable to quantify the amount of degradation. We recommend
                that you either enable short tags, convert your scripts to use
                full tags, or have a good partial and/or full page content
                caching strategy in place.
            </p>
        </div>
    </div>

    <div class="section" id="zend.view.introduction.accessors"><div class="info"><h1 class="title">Utility Accessors</h1></div>
        

        <p class="para">
            Typically, you&#039;ll only ever need to call on  <span class="methodname">assign()</span>,
             <span class="methodname">render()</span>, or one of the methods for setting/adding
            filter, helper, and script paths. However, if you wish to extend
            <span class="classname">Zend_View</span> yourself, or need access to some of its
            internals, a number of accessors exist:
        </p>

        <ul class="itemizedlist">
            <li class="listitem">
                <p class="para">
                     <span class="methodname">getVars()</span> will return all assigned variables.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">clearVars()</span> will clear all assigned variables;
                    useful when you wish to re-use a view object, but want to
                    control what variables are available.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getScriptPath($script)</span> will retrieve the
                    resolved path to a given view script.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getScriptPaths()</span> will retrieve all registered
                    script paths.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getHelperPath($helper)</span> will retrieve the
                    resolved path to the named helper class.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getHelperPaths()</span> will retrieve all registered
                    helper paths.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getFilterPath($filter)</span> will retrieve the
                    resolved path to the named filter class.
                </p>
            </li>

            <li class="listitem">
                <p class="para">
                     <span class="methodname">getFilterPaths()</span> will retrieve all registered
                    filter paths.
                </p>
            </li>
        </ul>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.view.html">Zend_View</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.view.html">Zend_View</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.view.controllers.html">Controller Scripts</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.view.html">Zend_View</a></li>
  <li class="active"><a href="zend.view.introduction.html">Introduction</a></li>
  <li><a href="zend.view.controllers.html">Controller Scripts</a></li>
  <li><a href="zend.view.scripts.html">View Scripts</a></li>
  <li><a href="zend.view.helpers.html">View Helpers</a></li>
  <li><a href="zend.view.abstract.html">Zend_View_Abstract</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>